package com.molyfun.parents.activity.setting.consult;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.mainpage.adapter.TrainRecommendAdapter;
import com.molyfun.parents.fragment.BaseFragment;
import com.molyfun.parents.im.MyTIMMsgListener;
import com.molyfun.parents.im.model.BaseMessageEntity;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.rest.bean.CreateCustomPlanResponse;
import com.molyfun.parents.rest.bean.CustomPlanlistResponse;
import com.molyfun.parents.view.swipeRefreshLayout.SwipeRefreshLayout;
import com.tencent.imsdk.TIMConversation;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.TIMManager;
import com.tencent.imsdk.ext.message.TIMConversationExt;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 设置-咨询管理
 */

public class ConsultFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener, View.OnClickListener, MyTIMMsgListener.ITIMMsgReceivedCallback {

    @InjectView(R.id.order_list)
    RecyclerView mRecyclerView;
    @InjectView(R.id.empty_layout)
    LinearLayout mEmptyLayout;
    @InjectView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @InjectView(R.id.consult)
    ImageView mConsult;

    TrainRecommendAdapter trainRecommendAdapter;

    LinearLayoutManager mLayoutManager;
    private int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean mHasMoreData = true;

    private SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            loadData(true);
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_consult, container, false);
        ButterKnife.inject(this, view);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);

        mLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mConsult.setOnClickListener(this);
        trainRecommendAdapter = new TrainRecommendAdapter(getContext());
        mRecyclerView.setAdapter(trainRecommendAdapter);
        loadData(true);

        return view;
    }


    @Override
    public void onMessage(BaseMessageEntity messageEntity) {
        switch (messageEntity.getEventtype()) {
            case MyTIMMsgListener.EventType.CUSTOMPLAN:
                if (messageEntity.getOpertype().equals(MyTIMMsgListener.OperType.REFRESH_COUNT)) {
                    onRefresh();
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.consult:
                createCustomplan();
                break;
        }
    }

    private CustomPlanlistResponse.PlanListBean planListBean;
    private String imgroupid;

    public void createCustomplan() {
        Call<CreateCustomPlanResponse> call = MFPApplication.getInstance().getRestClient().createCustomplan();
        call.enqueue(new Callback<CreateCustomPlanResponse>() {
            @Override
            public void onResponse(Call<CreateCustomPlanResponse> call, Response<CreateCustomPlanResponse> response) {
                dismissProgressDialog();
                if (response.body() != null) {
                    if (response.body().getCode() == 200) {
                        if (response.body() != null && response.body().getData() != null) {
                            planListBean = response.body().getData();
                            imgroupid = response.body().getData().getImgroupid();
//                            ChatActivity.startActivity(getContext(), response.body().getData(), response.body().getData().getImgroupid());
                            com.molyfun.parents.im.ConsultActivity.startActivity(getContext(), planListBean, imgroupid);
                        }
                    } else {
                        toast(response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<CreateCustomPlanResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });

    }

    @Override
    public void onRefresh() {
        loadData( true);
        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.setLoading(false);
    }

    @Override
    public void onLoad() {
        if (mHasMoreData) {
            mCurrentPage++;
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
            loadData(false);
        } else {
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MyTIMMsgListener.addITIMMsgReceivedCallback(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MyTIMMsgListener.removeITIMMsgReceivedCallback(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        MyTIMMsgListener.removeITIMMsgReceivedCallback(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        MyTIMMsgListener.removeITIMMsgReceivedCallback(this);
    }

    private void loadData(final boolean isRefresh) {
//        topicAdapter.notifyDataSetChanged();
        if (isRefresh) {
            mCurrentPage = RestClient.DEFAULT_PAGE;
        }
//        showProgressDialog();
        Call<CustomPlanlistResponse> call = MFPApplication.getInstance().getRestClient().getPlanList(mCurrentPage, RestClient.DEFAULT_PAGE_SIZE);
        call.enqueue(new Callback<CustomPlanlistResponse>() {
            @Override
            public void onResponse(Call<CustomPlanlistResponse> call, Response<CustomPlanlistResponse> response) {
//                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    for (CustomPlanlistResponse.PlanListBean planListBean : response.body().getData()) {
                        if (!"1".equals(planListBean.getStatus())) {
                            TIMConversation conversation = TIMManager.getInstance().getConversation(TIMConversationType.Group, planListBean.getImgroupid());
                            if (conversation != null) {
                                planListBean.setUnreadMessageNum(new TIMConversationExt(conversation).getUnreadMessageNum());
                            }
                        }
                    }
                    if (isRefresh) {
                        trainRecommendAdapter.clearAll();
                        if (response.body().getData().size() > 0) {
                            trainRecommendAdapter.refreshData(response.body().getData());
                            mHasMoreData = true;
                            mEmptyLayout.setVisibility(View.GONE);
                        } else {
                            mHasMoreData = false;
                            mEmptyLayout.setVisibility(View.VISIBLE);
                        }
                    } else {
                        if (response.body().getData().size() > 0) {
                            mHasMoreData = true;
                        } else {
                            mHasMoreData = false;
                        }
                        trainRecommendAdapter.addAll(response.body().getData());
                    }

                }
            }

            @Override
            public void onFailure(Call<CustomPlanlistResponse> call, Throwable t) {
//                dismissProgressDialog();
            }
        });

    }
}
